<%@ Page language="vb" CodeFile="settingsPriceList.aspx.vb" Inherits="settingsPriceList_aspx_vb" %>

<%
    rowNumber = 1
    If (Request.QueryString("do")) = Nothing Then 
        subDo = Request.QueryString("subDo")
        productID = Request.QueryString("productID")
        priceListID = Request.QueryString("priceListID")
        productID = CLng(productID)
%>
<script type="text/javascript">
  function confirmSubmit() { 
	if (window.confirm('Are you sure you want to delete the price?')) {
		location.href='settingsPriceList.aspx?do=delete&productID=<%= productID %>&priceListID=<%= priceListID %>';
		return false;
	}
	else {
		return false;
	}
  }
  </script>
<%
        dbOpen()
        RecSet = Conn.Execute("Select p.product, c.countryCurrency From (tblProduct p INNER JOIN tblCountry c ON c.countryID = p.countryID) Where p.productID = " & productID & "")
        If RecSet.EOF Then 
            Response.Redirect("settingsProduct.aspx")
        Else
            product = RecSet.Fields("product").Value
            countryCurrency = RecSet.Fields("countryCurrency").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If Len(priceListID) > 0 Then 
            priceListID = CLng(priceListID)
            dbOpen()
            RecSet = Conn.Execute("Select price, pricePerKmOverdue, adminFee, priceListCategory, priceListDuration, priceListDistance, uw, uwPerKmOverdue, cylinderVolumeFrom, cylinderVolumeTo, (Select Count(priceListID) From tblVehicle Where priceListID = " & priceListID & ") As vehicleCount From tblPriceList Where priceListID = " & priceListID & "")
            If RecSet.EOF Then 
                Response.Redirect("settingsProduct.aspx")
            Else
                dbPrice = RecSet.Fields("price").Value
                dbPricePerKmOverdue = RecSet.Fields("pricePerKmOverdue").Value
                dbAdminFee = RecSet.Fields("adminFee").Value
                dbPriceListCategory = RecSet.Fields("priceListCategory").Value
                dbPriceListDuration = RecSet.Fields("priceListDuration").Value
                dbPriceListDistance = RecSet.Fields("priceListDistance").Value
                dbUw = RecSet.Fields("uw").Value
                dbUwPerKmOverdue = RecSet.Fields("uwPerKmOverdue").Value
                dbCylinderVolumeFrom = RecSet.Fields("cylinderVolumeFrom").Value
                dbCylinderVolumeTo = RecSet.Fields("cylinderVolumeTo").Value
                dbVehicleCount = RecSet.Fields("vehicleCount").Value
            End IF
            RecSet.Close()
            RecSet = Nothing
            dbClose()
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""priceListCategory"", ""==''""],"
        strJS = strJS + "[""priceListDuration"", ""==''""],"
        strJS = strJS + "[""priceListDistance"", ""==''""],"
        strJS = strJS + "[""cylinderVolumeFrom"", ""==''""],"
        strJS = strJS + "[""cylinderVolumeTo"", ""==''""],"
        strJS = strJS + "[""price"", ""==''""],"
        strJS = strJS + "[""uw"", ""==''""],"
        strJS = strJS + "[""pricePerKmOverdue"", ""==''""],"
        strJS = strJS + "[""uwPerKmOverdue"", ""==''""],"
        strJS = strJS + "[""adminFee"", ""==''""],"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='100%'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>PRICE LIST SETTINGS FOR " & UCase(product) & "<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='settingsPriceList.aspx?do=save&productID=" & productID & "&priceListID=" & priceListID & "' method='post' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Category (A1):</td><td align='right'><input type='text' name='priceListCategory' id='priceListCategory' size='40' maxlength='50' value='" & dbPriceListCategory & "'></td><td><div id='error_priceListCategory' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Duration (month):</td><td align='right'><input type='text' name='priceListDuration' id='priceListDuration' size='40' maxlength='50' value='" & dbPriceListDuration & "' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_priceListDuration' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Distance (km):</td><td align='right'><input type='text' name='priceListDistance' id='priceListDistance' size='40' maxlength='50' value='" & dbPriceListDistance & "' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_priceListDistance' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Cylinder volume From (cc):</td><td align='right'><input type='text' name='cylinderVolumeFrom' id='cylinderVolumeFrom' size='40' maxlength='50' value='" & dbCylinderVolumeFrom & "' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_cylinderVolumeFrom' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Cylinder volume To (cc):</td><td align='right'><input type='text' name='cylinderVolumeTo' id='cylinderVolumeTo' size='40' maxlength='50' value='" & dbCylinderVolumeTo & "' onkeyup='re=/(\d*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_cylinderVolumeTo' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Price (" & countryCurrency & "):</td><td align='right'><input type='text' name='price' id='price' size='40' maxlength='50' value='" & dbPrice & "' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_price' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>UW (" & countryCurrency & "):</td><td align='right'><input type='text' name='uw' id='uw' size='40' maxlength='50' value='" & dbUw & "' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_uw' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Price / km overdue (" & countryCurrency & "):</td><td align='right'><input type='text' name='pricePerKmOverdue' id='pricePerKmOverdue' size='40' maxlength='50' value='" & dbPricePerKmOverdue & "' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_pricePerKmOverdue' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>UW / km overdue (" & countryCurrency & "):</td><td align='right'><input type='text' name='uwPerKmOverdue' id='uwPerKmOverdue' size='40' maxlength='50' value='" & dbUwPerKmOverdue & "' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_uwPerKmOverdue' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Administration Fee (" & countryCurrency & "):</td><td align='right'><input type='text' name='adminFee' id='adminFee' size='40' maxlength='50' value='" & dbAdminFee & "' onkeyup='re=/([\d\,]*)/; re.exec(this.value); this.value=RegExp.$1;'></td><td><div id='error_adminFee' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 And Len(priceListID) > 0 Then 
            Response.Write("<input type='button' value='Delete price' onClick='return confirmSubmit()'>&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("<tr><td><br></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td valign='top' colspan='3'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='100%'>")
        Response.Write("<tr><td class='headline' colspan='11'>CURRENT PRICE LIST FOR " & UCase(product) & "<hr class='hrHeadline'></td></tr>")
        dbOpen()
        RecSet = Conn.Execute("Select p.priceListID, p.price, p.pricePerKmOverdue, p.adminFee, p.priceListCategory, p.priceListDuration, p.priceListDistance, p.uw, p.uwPerKmOverdue, p.cylinderVolumeFrom, p.cylinderVolumeTo, c.countryCurrency, (Select Count(priceListID) From tblVehicle Where priceListID = p.priceListID) As vehicleCount1 , (Select Count(priceListID) From tblInvoiceContent Where priceListID = p.priceListID) As vehicleCount2 From ((tblPriceList p INNER JOIN tblProduct pr ON pr.productID = p.productID) INNER JOIN tblCountry c ON c.countryID = pr.countryID) Where p.productID = " & productID & " Order by p.priceListCategory, p.priceListDuration")
        If RecSet.EOF Then 
            Response.Write("<tr><td><font color='red'>No prices found.</font></td></tr>")
        Else
            Response.Write("<tr bgcolor='#eeeeee'>")
            Response.Write("<td class='formBold'>Category</td>")
            Response.Write("<td class='formBold' align='right'>Duration</td>")
            Response.Write("<td class='formBold' align='right'>Distance</td>")
            Response.Write("<td class='formBold' align='right'>Cylinder volume</td>")
            Response.Write("<td class='formBold' align='right'>Price</td>")
            Response.Write("<td class='formBold' align='right'>UW</td>")
            Response.Write("<td class='formBold' align='right'>Price / km overdue</td>")
            Response.Write("<td class='formBold' align='right'>UW / km overdue</td>")
            Response.Write("<td class='formBold' align='right'>Administration Fee</td>")
            Response.Write("<td class='formBold' align='right'>Number of contracts</td>")
            Response.Write("<td class='formBold' align='right'></td>")
            Response.Write("</tr>")
            Do Until RecSet.EOF
                priceListID = RecSet.Fields("priceListID").Value
                price = RecSet.Fields("price").Value
                pricePerKmOverdue = RecSet.Fields("pricePerKmOverdue").Value
                adminFee = RecSet.Fields("adminFee").Value
                PriceListCategory = RecSet.Fields("priceListCategory").Value
                PriceListDuration = RecSet.Fields("priceListDuration").Value
                PriceListDistance = RecSet.Fields("priceListDistance").Value
                Uw = RecSet.Fields("uw").Value
                uwPerKmOverdue = RecSet.Fields("uwPerKmOverdue").Value
                cylinderVolumeFrom = RecSet.Fields("cylinderVolumeFrom").Value
                cylinderVolumeTo = RecSet.Fields("cylinderVolumeTo").Value
                countryCurrency = RecSet.Fields("countryCurrency").Value
                VehicleCount1 = RecSet.Fields("vehicleCount1").Value
                VehicleCount2 = RecSet.Fields("vehicleCount2").Value
                VehicleCount = 0
                VehicleCount = CDbl(VehicleCount1) + CDbl(VehicleCount2)
                If rowNumber = 0 Then 
                    strGrey = " bgcolor='#eeeeee'"
                    rowNumber = 1
                Else
                    strGrey = ""
                    rowNumber = 0
                End IF
                Response.Write("<tr" & strGrey & ">")
                Response.Write("<td>" & PriceListCategory & "</td>")
                Response.Write("<td align='right'>" & PriceListDuration & " months</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(PriceListDistance), 0) & " km</td>")
                Response.Write("<td align='right'>" & cylinderVolumeFrom & " - " & cylinderVolumeTo & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(price), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(Uw), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(pricePerKmOverdue), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(uwPerKmOverdue), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(adminFee), 2) & " " & countryCurrency & "</td>")
                Response.Write("<td align='right'>" & FormatNumber(CDbl(VehicleCount1), 0) & "</td>")
                If CInt(VehicleCount) = 0 Then 
                    Response.Write("<td align='right'><a href='settingsPriceList.aspx?productID=" & productID & "&priceListID=" & priceListID & "'>edit price</a></td>")
                Else
                    Response.Write("<td align='right'></td>")
                End IF
                Response.Write("</tr>")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<tr><td colspan='11'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        price = Request.Form("price")
        pricePerKmOverdue = Request.Form("pricePerKmOverdue")
        adminFee = Request.Form("adminFee")
        PriceListCategory = Request.Form("priceListCategory")
        PriceListDuration = Request.Form("priceListDuration")
        PriceListDistance = Request.Form("priceListDistance")
        Uw = Request.Form("uw")
        uwPerKmOverdue = Request.Form("uwPerKmOverdue")
        cylinderVolumeFrom = Request.Form("cylinderVolumeFrom")
        cylinderVolumeTo = Request.Form("cylinderVolumeTo")
        productID = Request.QueryString("productID")
        priceListID = Request.QueryString("priceListID")
        productID = CLng(productID)
        If Len(price) > 0 Then 
            price = Trim(price)
            price = Replace(CStr(price), ",", ".")
        Else
            price = 0
        End IF
        If Len(pricePerKmOverdue) > 0 Then 
            pricePerKmOverdue = Trim(pricePerKmOverdue)
            pricePerKmOverdue = Replace(CStr(pricePerKmOverdue), ",", ".")
        Else
            pricePerKmOverdue = 0
        End IF
        If Len(adminFee) > 0 Then 
            adminFee = Trim(adminFee)
            adminFee = Replace(CStr(adminFee), ",", ".")
        Else
            adminFee = 0
        End IF
        If Len(PriceListCategory) > 0 Then 
            PriceListCategory = Trim(PriceListCategory)
            PriceListCategory = Replace(CStr(PriceListCategory), "'", "")
            PriceListCategory = Replace(CStr(PriceListCategory), """", "")
            PriceListCategory = Replace(CStr(PriceListCategory), "<", "")
            PriceListCategory = Replace(CStr(PriceListCategory), ">", "")
            PriceListCategory = Replace(CStr(PriceListCategory), "=", "")
        End IF
        If Len(Uw) > 0 Then 
            Uw = Trim(Uw)
            Uw = Replace(CStr(Uw), ",", ".")
        Else
            Uw = 0
        End IF
        If Len(uwPerKmOverdue) > 0 Then 
            uwPerKmOverdue = Trim(uwPerKmOverdue)
            uwPerKmOverdue = Replace(CStr(uwPerKmOverdue), ",", ".")
        Else
            uwPerKmOverdue = 0
        End IF
        If Len(priceListID) > 0 Then 
            priceListID = CLng(priceListID)
            dbOpen()
            Response.Write("Update tblPriceList Set price = " & price & ", pricePerKmOverdue = " & pricePerKmOverdue & ", adminFee = " & adminFee & ", priceListCategory = '" & PriceListCategory & "', priceListDuration = " & PriceListDuration & ", priceListDistance = " & PriceListDistance & ", uw = " & Uw & ", uwPerKmOverdue = " & uwPerKmOverdue & ", cylinderVolumeFrom = " & cylinderVolumeFrom & ", cylinderVolumeTo = " & cylinderVolumeTo & " Where priceListID = " & priceListID & "")
            Conn.Execute("Update tblPriceList Set price = " & price & ", pricePerKmOverdue = " & pricePerKmOverdue & ", adminFee = " & adminFee & ", priceListCategory = '" & PriceListCategory & "', priceListDuration = " & PriceListDuration & ", priceListDistance = " & PriceListDistance & ", uw = " & Uw & ", uwPerKmOverdue = " & uwPerKmOverdue & ", cylinderVolumeFrom = " & cylinderVolumeFrom & ", cylinderVolumeTo = " & cylinderVolumeTo & " Where priceListID = " & priceListID & "")
            dbClose()
        Else
            dbOpen()
            Conn.Execute("Insert Into tblPriceList (productID, price, pricePerKmOverdue, adminFee, priceListCategory, priceListDuration, priceListDistance, uw, uwPerKmOverdue, cylinderVolumeFrom, cylinderVolumeTo) Values(" & productID & "," & price & "," & pricePerKmOverdue & "," & adminFee & ",'" & PriceListCategory & "'," & PriceListDuration & "," & PriceListDistance & "," & Uw & "," & uwPerKmOverdue & "," & cylinderVolumeFrom & "," & cylinderVolumeTo & ")")
            dbClose()
        End IF
        Response.Redirect("settingsPriceList.aspx?productID=" & productID & "&subDo=%20-%20SPARAD")
    ElseIf Request.QueryString("do") = "delete" Then 
        productID = Request.QueryString("productID")
        priceListID = Request.QueryString("priceListID")
        productID = CLng(productID)
        priceListID = CLng(priceListID)
        dbVehicleCount = 0
        dbOpen()
        RecSet = Conn.Execute("Select Count(vehicleID) As vehicleCount From tblVehicle Where PriceListID = " & priceListID & "")
        dbVehicleCount = RecSet.Fields("vehicleCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        VehicleCount = VehicleCount + dbVehicleCount
        dbOpen()
        RecSet = Conn.Execute("Select Count(vehicleID) As vehicleCount From tblInvoiceContent Where PriceListID = " & priceListID & "")
        dbVehicleCount = RecSet.Fields("vehicleCount").Value
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        VehicleCount = VehicleCount + dbVehicleCount
        If CInt(dbVehicleCount) = 0 And CDbl(fragusLeasingUserTypeID) = 1 Then 
            dbOpen()
            Conn.Execute("Delete From tblPriceList Where priceListID = " & priceListID & "")
            dbClose()
        End IF
        Response.Redirect("settingsPriceList.aspx?productID=" & productID & "&subDo=%20-%20RADERAD")
    End IF
%>
